package com.emergency.db.controller;


import com.emergency.common.entity.Result;
import com.emergency.common.entity.StatusCode;
import com.emergency.common.entity.TreeNode;
import com.emergency.db.domain.Dictionary;
import com.emergency.db.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//数据字典
@RestController
@RequestMapping("/dictionary")
@CrossOrigin
public class DictionaryController {
    @Autowired
    private DictionaryService dictionaryService;

    //保存
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody Dictionary dictionary) {
        Long id = dictionaryService.save(dictionary);
        return new Result(true, StatusCode.OK, "保存成功", id);
    }

    //查询单项
    @RequestMapping(value = "/search/{id}", method = RequestMethod.POST)
    public Result findById(@PathVariable("id") Long id) {
        Dictionary dictionary = dictionaryService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", dictionary);
    }

    //查询全部字典信息
    @RequestMapping(value = "/search/all", method = RequestMethod.POST)
    public Result findAll() {
        List<Dictionary> dictionaries = dictionaryService.findALL();
        return new Result(true, StatusCode.OK, "查询成功", dictionaries);
    }

    //构造树形格式
    @RequestMapping(value = "/tree", method = RequestMethod.POST)
    public Result tree() {
        List<TreeNode> treeNode = dictionaryService.getTreeData();
        return new Result(true, StatusCode.OK, "查询成功", treeNode);
    }

    //删除
    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST)
    public Result delete(@PathVariable Long id) {
        dictionaryService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }


    @RequestMapping(value = "list/{page}/{size}", method = RequestMethod.POST)
    public Result list(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Dictionary> pageList = dictionaryService.findList(searchMap, page, size);
        return new Result(true, StatusCode.OK, "操作成功", pageList);
    }



}
